package org.hhchat.leetcode.tree;

import org.hhchat.leetcode.dataStruct.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created this one by HMH on 2017/10/3.
 */
public class code653_easy {
    static class Solution {
        public boolean findTarget(TreeNode root, int k) {
            visit(root);
            int p = 0;
            int q = list.size() - 1;
            while(p<q){
                int sum = list.get(p) + list.get(q);
                if (sum == k) {
                    return true;
                }
                if (sum > k) {
                    q--;
                }else{
                    p++;
                }
            }
            return false;
        }

        List<Integer> list = new ArrayList<>();

        public void visit(TreeNode treeNode) {
            if (treeNode == null) {
                return;
            }
            visit(treeNode.left);
            list.add(treeNode.val);
            visit(treeNode.right);

        }
    }


}
